package com.shsoft.myThread;

import com.shsoft.colorfulcc.MainActivity;
import com.shsoft.common.Constant;

/**
 * 混色-常亮 子线程
 */
public class MixLongThread extends Thread{
    private MainActivity activity;
    private int interval; // 亮灯时间 （1-250）*100ms
    private int offtime; // 灭灯时间 （1-250）*100ms
    private int bright; // 记录亮度

    public MixLongThread(MainActivity activity) {
        this.activity = activity;
        bright = Integer.parseInt(activity.long_bright.getText().toString());
        if(bright * 2 > 255){
            bright = 255;
        }else {
            bright = bright * 2;
        }
        interval = Integer.parseInt(activity.long_interval.getText().toString()) * 100;
        offtime = Integer.parseInt(activity.long_offtime.getText().toString()) * 100;
    }

    @Override
    public void run() {
        System.out.println("混色常亮灯子线程运行");
        while ( activity.isPlaying){
            // 将灯珠的颜色全部换成要显示的颜色
            for(int i = 0; i < Constant.LIGHT_COUNT; i++){
                int colorIndex = activity.lights.get(i);
                if(colorIndex == 20 || colorIndex >= activity.colors.size()){
                    activity.lights_4display.put(i,new int[]{1,1,1});
                }else {
                    activity.lights_4display.put(i,activity.colors.get(colorIndex));
                }
            }
            // 让主线程刷新灯珠区域
            activity.alpha = bright;
            activity.refreshLightsDiv();
            // 睡眠 interval
            try {
                sleep(interval);
            } catch (InterruptedException e) {
                e.printStackTrace();
                break;
            }
            if(offtime != 0){ // 需要换色灭灯
                int rgb[] = new int[]{1,1,1};
                for(int i = 0; i < Constant.LIGHT_COUNT; i++){
                    activity.lights_4display.put(i,rgb);
                }
                // 让主线程刷新灯珠区域
                activity.alpha = 250;
                activity.refreshLightsDiv();
                // 睡眠 interval
                try {
                    sleep(offtime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }
            }
        }
    }
}
